package com.xm.service.controller;

import com.xm.service.framework.util.Dateutil;
import com.xm.service.module.BOOK.po.Book;

import com.xm.service.module.BOOK.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;


    @RequestMapping("/list")
    public List<Book> list(){
        return bookService.list();
    }

    @GetMapping("/get")
    public Book getById(@RequestParam int id){
        return bookService.getById(id);
    }

    @PostMapping("/add")
    public String  add(@RequestParam String author, @RequestParam int stock,
                       @RequestParam String name,  @RequestParam String date){/*@PathVariable String publish_date*/
        final Book book = new Book();
        Dateutil dateutil = new Dateutil();
        Date sqlDate = dateutil.StringToDate(date);

        book.setBookAuthor(author);
        book.setBookName(name);
        book.setInStock(stock);
        book.setPublishDate(sqlDate);
        bookService.save(book);
        return "ok";
    }



    @DeleteMapping("/delete")
    public String deleteById(@RequestParam int id){
        if (bookService.deleteById(id)){
            return "ok";
        }else {
            return "删除失败";
        }
    }
    @PutMapping("/update")
    public String updateStockByid(@RequestParam int id,@RequestParam int stock){

        if (bookService.updateById(id,stock)){
            return "ok";
        }else return "更新失败";
    }

}
